using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace mirror
{
    public class Actor : cRigidRenderable
    {
        public float heading;
        public Vector2 pos;
        public Vector2 vel;
        public Vector2 acc;
        public float health = 5.0f;
        public Boolean isDead = false;
        public int score;

        public JigLibX.Physics.Body body;
        public JigLibX.Collision.CollisionSkin collision;

        public Actor(Model model, Vector2 position)
            : base(model)
        {
            pos = position;
            body = new JigLibX.Physics.Body();
            
            collision = new JigLibX.Collision.CollisionSkin(body);
            collision.AddPrimitive(new JigLibX.Geometry.Sphere(Vector3.Zero, 0.5f), new JigLibX.Collision.MaterialProperties(0.5f, 10.0f, 30.0f));
            body.CollisionSkin = collision;
            body.MoveTo(new Vector3(pos.X, 10.0f, pos.Y), Matrix.Identity);
            body.AllowFreezing = false;
        }

        public virtual void Update()
        {
            // Scale
            vel *= 0.3f;
            acc *= 0.1f;

            vel += acc;
            acc *= 0.5f;
            pos += vel;
            body.SetActive();

            if (acc.Length() > 0.0f)
            {
                while (false) ;
            }

            //mTransform = Matrix.CreateScale(0.005f);
            //mTransform *= Matrix.CreateRotationY(heading) * Matrix.CreateTranslation(pos.X, 0.5f, pos.Y); // *Matrix.CreateScale(0.01f);

            //mTransform = Matrix.CreateRotationY(heading) * Matrix.CreateScale(0.005f) * Matrix.CreateTranslation(pos.X, 0.5f, pos.Y);//body.Position);

            body.ApplyWorldImpulse(new Vector3(vel.X, 0.0f, vel.Y));
            pos.X = body.Position.X;
            pos.Y = body.Position.Z;
            mTransform = Matrix.CreateRotationY(heading) * Matrix.CreateScale(0.06f) * Matrix.CreateTranslation(body.Position);
        }

        public virtual void OnHit( float damage )
        {
            health -= damage;

            if (health <= 0.0f)
            {
                isDead = true;
            }
        }

        public override void Render()
        {
            base.Render();
        }
    };
}